#!/bin/bash

TOMCAT_PATH='/home/linux_7230263118/tomcat9'
EXAM_PATH='/home/linux_7230263118/Exam'

# 步骤1: 准备CP变量
CP="${TOMCAT_PATH}/lib/servlet-api.jar"

# 添加webapps/ROOT/WEB-INF/lib下的所有jar包
for jar in ${TOMCAT_PATH}/webapps/ROOT/WEB-INF/lib/*.jar; do
    CP="${CP}:${jar}"
done

# 步骤2: 编译
echo "开始编译Java文件..."
javac -cp "${CP}" -d ${EXAM_PATH}/target ${EXAM_PATH}/code/*.java

if [ $? -ne 0 ]; then
    echo "编译失败!"
    exit 1
fi

echo "编译成功!"

# 步骤3: 部署
echo "开始部署class文件..."
cp -rf ${EXAM_PATH}/target/* ${TOMCAT_PATH}/webapps/ROOT/WEB-INF/classes/

if [ $? -ne 0 ]; then
    echo "部署失败!"
    exit 1
fi

echo "部署成功!"

# 步骤4: 重启Tomcat
echo "重启Tomcat..."
${TOMCAT_PATH}/bin/shutdown.sh
sleep 5
${TOMCAT_PATH}/bin/startup.sh

echo "部署完成!"
